#include "str_echo.h"
#include "sum.h"
#include <define.h>
#include <sockfunc.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>


void 
str_echo(int connfd) {
	ssize_t n;
	char buf[MAX_LINE];
	for(;;) {
		while ((n=read(connfd, buf, sizeof(buf))) > 0) {
			Writen(connfd, buf, n);
		}

		if (n < 0 && errno == EINTR)
			continue;
		else
			break;
	}
}

void
str_echo08(int connfd) {
	int32_t a, b;
	char buf[MAX_LINE];
	for (;;) {
		if (readline(connfd, buf, sizeof(buf)) <= 0)
			return;
		if (sscanf(buf, "%d%d", &a, &b) == 2) {
			snprintf(buf, sizeof(buf), "%ld\n", (int64_t)a + b);
		} else {
			snprintf(buf, sizeof(buf), "invalid input\n");
		}

		Writen(connfd, buf, strlen(buf));
	}
}

void
str_echo09(int connfd) {
	struct SumArgs args;
	struct SumResult result;
	for (;;) {
		Readn(connfd, &args, sizeof(args));
		result.sum = args.arg1 + args.arg2;
		Writen(connfd, &result, sizeof(result));
	}
}
